package com.muyan.sys.common.utils;

import java.time.*;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class LocalDateUtils {

    /**
     * 获取当天日期
     *
     * @return
     */
    public static int getDay(){

        LocalDate now = LocalDate.now();


        int dayOfMonth = now.getDayOfMonth();

        return dayOfMonth;

    }

    /**
     * 获取截止昨天往前7天的开始结束时间
     *
     * @return
     */
    public static Date[] getDateNear(){
        Date[]  dates=new Date[2];
        LocalDate now = LocalDate.now();

        LocalDate start = now.now().plusDays(-7);
        LocalDateTime end = LocalDateTime.now().plusDays(-1).with(LocalTime.MAX);

        return getDates(dates, end, start);

    }

    /**
     * 获取周开始结束时间 yyyy-MM-dd
     *
     * @return
     */
    public static Date[] getDateWeek(){
        Date[]  dates=new Date[2];
        LocalDate now = LocalDate.now();
        int dayOfWeek = now.getDayOfWeek().getValue();
        LocalDate weekStart = now.minusDays(dayOfWeek - 1);
        LocalDate weekEnd = now.plusDays(7 - dayOfWeek);

        return getDates(dates, weekEnd, weekStart);

    }


    /**
     * 获取月开始结束时间 yyyy-MM-dd
     *
     * @return
     */
    public static Date[] getDateMonth(){
        Date[]  dates=new Date[2];
        LocalDate now = LocalDate.now();
        LocalDate localStart = now.with(TemporalAdjusters.firstDayOfMonth());
        LocalDate localEnd = now.with(TemporalAdjusters.lastDayOfMonth());

        return getDates(dates, localEnd, localStart);

    }
    /**
     * 获取年开始结束时间 yyyy-MM-dd
     *
     * @return
     */
    public static Date[] getDateYear(){
        Date[]  dates=new Date[2];
        LocalDate now = LocalDate.now();


        LocalDate localStart = now.with(TemporalAdjusters.firstDayOfYear());
        LocalDate localEnd = now.with(TemporalAdjusters.lastDayOfYear());

        return getDates(dates, localEnd, localStart);

    }

    /**
     * 获取今年开始时间到现在的时间 boolean：true-今年 flase：去年
     *
     * @return
     */
    public static Date[] getDateYearToNOW(boolean b){
        Date[]  dates=new Date[2];
        LocalDate now=null;
        if(b){
            now = LocalDate.now();
        }else{
            now = LocalDate.now().plusYears(-1);
        }
        LocalDate localStart = now.with(TemporalAdjusters.firstDayOfYear());
        return getDates(dates, now, localStart);
    }

    private static Date[] getDates(Date[] dates, LocalDate now, LocalDate localStart) {
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdtStart = localStart.atStartOfDay(zoneId);
        ZonedDateTime zdtEnd= now.atStartOfDay(zoneId);
        Date start = Date.from(zdtStart.toInstant());
        Date end = Date.from(zdtEnd.toInstant());
        dates[0]=start;
        dates[1]=end;
        return dates;
    }
    private static Date[] getDates(Date[] dates, LocalDateTime now, LocalDate localStart) {
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdtStart = localStart.atStartOfDay(zoneId);
        ZonedDateTime zdtEnd= now.atZone(zoneId);
        Date start = Date.from(zdtStart.toInstant());
        Date end = Date.from(zdtEnd.toInstant());
        dates[0]=start;
        dates[1]=end;
        return dates;
    }
    /**
     * 获取当月开始时间到现在的时间 boolean：true-当前月 flase：上个月
     *
     * @return
     */
    public static Date[] getDateMonthToNOW(boolean b){
        Date[]  dates=new Date[2];
        LocalDate now=null;
        if(b){
            now = LocalDate.now();
        }else{
            now = LocalDate.now().plusMonths(-1);
        }
        LocalDate localStart = now.with(TemporalAdjusters.firstDayOfMonth());
        return getDates(dates, now, localStart);
    }


    /**
     * 获取星期
     * @return
     */
    public static int getNowWeek(){
        LocalDate now = LocalDate.now();
        return now.getDayOfWeek().getValue();
    }


    /**
     * 获取年
     * @return
     */
    public static int getNowYear(){

        LocalDate now = LocalDate.now();



        return now.getYear();

    }
    /**
     * 获取年
     * @return
     */
    public static int getYear(int year){

        LocalDate lastYear  = LocalDate.now().plusYears(year);



        return lastYear.getYear();

    }
    public static String getYear(Date dateTime){


        LocalDate  date=convertDateToLocalDate(dateTime);


        return date.getYear()+"";

    }

    /**
     * 获取近几个年的年份
     * @param index
     * @return
     */
    public static List<String>  getYearArr(int index){

            List<String> list=new ArrayList<>();
        int minyear=getYear(-index);
        for(int i=minyear+1;i<=minyear+5;i++){
            list.add(i+"");
        }


        return list;

    }
    /**
     * 获取年
     * @return
     */
    public static int getNowMonth(){

        LocalDate now = LocalDate.now();



        return now.getMonthValue();

    }
    /**
     * 获取月
     * @return
     */
    public static Integer getMonth(int i){
        LocalDate date  = LocalDate.now().plusMonths(i);

        return date.getMonthValue();

    }
    /**
     * 获取月
     * @return
     */
    public static Integer getMonthYear(int i){
        LocalDate date  = LocalDate.now().plusMonths(i);

        return date.getYear();

    }
    /**
     * 获取年
     * @return
     */
    public static String getMonth(Date dateTime){
        LocalDate  date=convertDateToLocalDate(dateTime);




        return date.getMonthValue()+"";

    }
    /**
     * 上个月的月开始时间、当前时间上个月时间
     * @return
     */
    public static String lastMonthByDay(String year,String month) {

        LocalDate localDate= LocalDate.of(Integer.valueOf(year),Integer.valueOf(month),1);
        localDate=  localDate.minusMonths(-1);
        return localDate.getYear()+"-"+localDate.getMonthValue();
    }
    /**
	 * 上个月的月开始时间、当前时间上个月时间
	 * @return
	 */
    public static Date[] lastMonth() {
		return lastMonth(-1);
	}
	public static Date[] lastMonth(int month) {
		Date[]  dates=new Date[2];
		LocalDate lastYear  = LocalDate.now().plusMonths(month);
		LocalDate localStart = lastYear.with(TemporalAdjusters.firstDayOfMonth());
		LocalDate localEnd = null;
		 //判断当前时间是否是月末
		 if (monthEnd()) {
			//去年结束时间也取月末
			 localEnd = lastYear.with(TemporalAdjusters.lastDayOfMonth());
		 }else {
			 localEnd = lastYear;
		}
	    return getDates(dates, localEnd, localStart);
	}
	/**
	 * 上个星期
	 * @return
	 */
	public static Date[] lastWeek() {
		return lastWeek(-1);
	}
	public static Date[] lastWeek(int week) {
		if (week==0) {
			week = -1;
		}
		Date[]  dates=new Date[2];
		LocalDate lastStart  = LocalDate.now().plusDays(7*week);
		LocalDate lastEnd  = LocalDate.now().plusDays(1*week);
		return getDates(dates, lastEnd, lastStart);
	}
	/**
	 * 去年年开始时间、当前时间的去年时间
	 * @return
	 */
	public static Date[] lastYear() {
		return lastYear(-1);
	}
	public static Date[] lastYear(int year) {
		Date[]  dates=new Date[2];
		LocalDate lastYear  = LocalDate.now().plusYears(year);
		LocalDate localStart = lastYear.with(TemporalAdjusters.firstDayOfYear());
		LocalDate localEnd = null;
		 //判断当前时间是否是月末
		 if (monthEnd()) {
			//去年结束时间也取月末
			 localEnd = lastYear.with(TemporalAdjusters.lastDayOfMonth());
		 }else {
			 localEnd = lastYear;
		}
	    return getDates(dates, localEnd, localStart);
	}
	/**
	 * 当前时间的去年月开始时间
	 * 当前时间的去年时间
	 * @return
	 */
	public static boolean monthEnd() {
		Integer monthEnd = LocalDate.now().lengthOfMonth();
		Integer dayMont = LocalDate.now().getDayOfMonth();
		if (monthEnd.equals(dayMont)) {
		    return true;
		}else{
		    return false;
		}
	}
	/**
	 * 0当前时间的 季度
	 * -1 当前时间的上个季度
	 * @return
	 */
	public static Date[] quarter() {
		return quarter(-1);
	}
	public static Date[] quarter(int quarter) {
		Date[]  dates=new Date[2];
        LocalDate now = LocalDate.now().plusMonths(quarter*3);
        int quaterNum = (now.getMonthValue()+2)/3;
        int endQ = now.getMonthValue();
        int startQ = quaterNum*3-2;
        LocalDate localStart = now.plusMonths(-(endQ-startQ)).with(TemporalAdjusters.firstDayOfMonth());
        //判断当前时间是否是月末
        if (monthEnd()) {
        	now.with(TemporalAdjusters.lastDayOfMonth());
		}
        return getDates(dates, now, localStart);
	}


    public static String getYearAndQuarter(Date dateTime) {
        LocalDate  date=convertDateToLocalDate(dateTime);
        int year = date.getYear();
        int month = date.getMonth().getValue();
        int quater=(month-1)/3+1;
        return year + "-" + quater;
    }
    public static String getQuarter(Date dateTime) {
        LocalDate  date=convertDateToLocalDate(dateTime);
        int year = date.getYear();
        int month = date.getMonth().getValue();
        int quater=(month-1)/3+1;
        return quater+"";
    }





    public static List<String> getRecentQuartersYear( int numQuarters) {
        Date dateTime=new Date();
        LocalDate  date=convertDateToLocalDate(dateTime);

        int currentQuarter=(date.getMonthValue()-1)/3+1;
        LocalDate startDate=convertDateToLocalDate(quarter(currentQuarter)[0]);
         startDate=startDate.minusMonths((currentQuarter - 1) * 3+1);
         startDate=startDate.minusMonths((numQuarters* 3));

        List<String> quarters = new ArrayList<>();

        int startQuarter= 1;
        int endQuarter = startQuarter + numQuarters - 1;
        for (int i = startQuarter; i <= endQuarter; i++) {
            LocalDate startOfQuarter =   startDate.plusMonths(1);;
            LocalDate endOfQuarter = startDate.plusMonths(3);
            quarters.add("Quarter " + i + ": " + startOfQuarter + " - " + endOfQuarter);
            startDate = endOfQuarter;
        }
        return quarters;
    }

    public static LocalDate convertDateToLocalDate(Date date) {
        // 将Date转换为Instant
        Instant instant = date.toInstant();

        // 将Instant转换为ZonedDateTime，使用系统默认的时区
        ZoneId defaultZoneId = ZoneId.systemDefault();
        ZonedDateTime zonedDateTime = instant.atZone(defaultZoneId);

        // 将ZonedDateTime转换为LocalDate
        LocalDate localDate = zonedDateTime.toLocalDate();
        return localDate;
    }



}
